package fnord.jovian.tango;

import java.util.Vector;

public class TheAssemblyLayout extends TheLayout {
	
	public enum TheAssemblyOrientation {VERTICAL,HORIZONTAL};
	private TheAssemblyOrientation _orientation = TheAssemblyOrientation.HORIZONTAL;
	private double _padding = 50;
	
	public TheAssemblyLayout() {}
	
	public TheAssemblyLayout(TheAssemblyOrientation orientation) {
		_orientation = orientation;
	}
	
	public TheAssemblyLayout(TheAssemblyOrientation orientation, int padding) {
		_orientation = orientation;
		_padding = padding;
	}
	
	public void setOrientation(TheAssemblyOrientation orientation) {
		_orientation = orientation;
	}
	
	public void setPadding(double padding) {
		_padding = padding;
	}
	
	public double getPadding() {
		return _padding;
	}

	public void layout(Vector<TheObject> v, Vector<TheOrientation> or, double x, double y, double w, double h) {
		if (_orientation == TheAssemblyOrientation.HORIZONTAL) {
			double totalWidth = 0;
			for (int i = 0; i < v.size(); i++)
				totalWidth += v.get(i).getWidth();
			double padding = (w - 2*_padding - totalWidth)/(double)(v.size()-1);
			x += _padding;
			for (int i = 0; i < v.size(); i++) {
				TheObject o = v.get(i);
				double oh = o.getHeight();
				o.setLocation(x, y + (h - oh)/2.0);
				x += padding + o.getWidth();
			}
		}
		else {
			double totalHeight = 0;
			for (int i = 0; i < v.size(); i++)
				totalHeight += v.get(i).getHeight();
			double padding = (h - 2*_padding - totalHeight)/(double)(v.size()-1);
			y += _padding;
			for (int i = 0; i < v.size(); i++) {
				TheObject o = v.get(i);
				double ow = o.getWidth();
				o.setLocation(x + (w - ow)/2.0, y);
				y += padding + o.getHeight();
			}
		}
	}

}